<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.model.Class" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Subject" %>
<%@ page import ="com.easyattendance.model.SubjectClass" %>
<html>
<head>
<title>Teacher - Add Material</title>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> -->
<jsp:include page="../includeFiles.jsp" />

</head>

<%
	String currentUser=(String)session.getAttribute("currentUser");
	AddTeacherService teacherService;	
	String teacherId=null;
if(currentUser!=null)
	{
		teacherService=new AddTeacherService();
		teacherId=teacherService.getTeacherByEmailId(currentUser).getTeacher_id();
	}	

%>
<body class="left-sidebar" onload="getValues()">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp" />
<jsp:include page="../common/checkTeacherRole.jsp" />
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">

				<!-- Login -->
						<div id="login_content">
						<header>
							<h2>Add Material</h2>
						</header>
						
						<span class="error_msg">${materialExists}</span>
						<span class="success_msg">${materialAdded}</span>
						<span class="error_msg">${materialNotAdded}</span>
						<span class="error_msg">${file_upload_error}</span>
						<span class="error_msg">${null_values}</span>
						<div class="submit_errors_msg">
							<ul>
								<li id="submit_is_field_empty" class="submit_errors">All * fields are required.</li>
							</ul>
						</div>
						<form name="addMaterialForm" action="${pageContext.request.contextPath}/AddMaterialServlet" method="post" enctype="multipart/form-data"  onsubmit="return checkFormErrors();">
							<!-- Hidden field -->
							<input type="hidden" id="action_type" name="action_type" value="create">
							
							<!-- Material of which class -->
							<div>
								<label> Class: <span class="error_msg">*</span></label>
								<select name="select_class" id="select_class" tabindex="1" value=<script>getCookie("select_class")</script> >
									<option value="">Select Class
										<% AddClassService addClassService = new AddClassService();	
									List<SubjectClass> classList = new ArrayList<SubjectClass>();
									classList = addClassService.listClassesByTeacherId(teacherId);
									SubjectClass tempClass = null;
									Iterator classItr = classList.iterator();
								
									while(classItr.hasNext()) { %>
										<% tempClass = (SubjectClass) classItr.next();
											String classId = tempClass.getClass_id();
											//String className = tempClass.getClassObj().getClass_name();%>
										<option value=<%=classId %>><%= classId %>
									<% } %>
								</select>
							</div>
							
							<%
								String classId=request.getParameter("class");
							%>
							
							<input id ="class_id" name="class_id" type="hidden" value="<%=classId %>">
							
							<!-- Subjects depend upon class selected -->
							<div>
							  	<label>Subject: <span class="error_msg">*</span></label>
								<select name="subject_name" id="subject_name" tabindex="2">
									<option value="">Select Subject
									<% AddSubjectService addSubjectService = new AddSubjectService();	
									List<Subject> subjectList = new ArrayList<Subject>();
									subjectList = addSubjectService.listSubjectsByTeacherIdForClass(teacherId,classId);
									//subjectList=addSubjectService.listSubjects();
									String tempSubject = null;
									Iterator subjectItr = subjectList.iterator();
								  
									while(subjectItr.hasNext()) { %>
										<% tempSubject =(String) subjectItr.next();
											String subjectName = addSubjectService.getSubjectBySubjectId(tempSubject).getSubject_name(); %>
										<option value='<%=subjectName %>'><%= subjectName %>
									<% } %>
								</select>
							</div>
							
							<!-- Material Number-->
							<div>
								<label> Material Number:<span class="error_msg">*</span></label>
								<input type="text" id="material_id" name="material_id" tabindex="3" placeholder="Material Number">
							</div>
							
							<!-- Name of the material -->
							<div>
								<label> Material Name:<span class="error_msg">*</span></label>
								<input type="text" id="material_name" name="material_name" tabindex="4" placeholder="Material Name">
							</div>
							
							<!-- Categories depend upon Subject selected -->
							<div>
								<label> Category: <span class="error_msg">*</span></label>
								<select name="category" id="category" tabindex="5">
									<option value="">Select Category
									<option value='Books'>Books
									<option value='Papers'>Papers
									<option value='Syllabus'>Syllabus
									<option value='Documents'>Documents
									<option value='Result'>Result
								</select>
							</div>
							<div>
								<label>File Location: <span class="error_msg">*</span></label>
							</div>
							<div> 
								<input id="file_name" name="file_name" type="text" placeholder="File Location" tabindex="6" style="width: 70%;float: left;margin-right: 2%;">
								<input type="file" value="Browse" name="fileName" class="button browse_button" tabindex="7">
							</div>
							
							<!--  hidden fields -->
								<input type="hidden" name="teacher_id" id="teacher_id" value="<%= teacherId %>">
								<input type="hidden" id="user_type" name="user_type" value="teacher">
							
							<div>	
								<input type="submit" value="Upload File" class="button" tabindex="8">
								<!-- <input type="button" value="Cancel" class="button"> -->
							</div>
						</form>
					</div>
			
			</div>
		</div>

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>	
	<script type="text/javascript">
		$(function(){
			$("#materialSettings").addClass("current_page_item");
		});
		var contextPath='<%=request.getContextPath()%>';
	</script>
	
	<!-- To get the uploaded file path in textbox -->
	<script language="javascript">     

     $(document).ready(function () {       

          $('input[name="fileName"]').change(function () {           

          var selectedValue = $(this).val();              

         $('input[name="file_name"]').val(selectedValue);          

         });           

     });
</script>

<!--  For drop down values -->
</script>
	<script type="text/javascript">
	$(".submit_errors_msg").hide();
	$(".submit_errors").hide();
	
	function checkFormErrors(){
		if(isAnyFieldEmpty($('#select_class'),$('#subject_name'),$('#category'),$('#file_name'))){
			return true;
		}
		return false;
	}
	
	
		$(function () {
		    var temp = 0;
		    $('#select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#select_class").val();
				setCookie("select_class",select_class);
				window.location.replace(contextPath + "/views/teacher/addMaterial.jsp?class=" + select_class);
		    });      
		});
		
		
		$(function () {
		    var temp = 0;
		    $('#select_subject').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#select_class").val();
				var select_subject = $("#select_subject").val();
				setCookie("select_subject",select_subject);
				window.location.replace(contextPath + "/views/teacher/addMaterial.jsp?" + "class=" + select_class + "&subject=" + select_subject);
		    });      
		});
		
				
		function getValues(){
			$("#select_class").val(getCookie("select_class"));
			$("#select_subject").val(getCookie("select_subject"));
		}
		
		function getCookie(name) { 
			var re = new RegExp(name + "=([^;]+)"); 
			var value = re.exec(document.cookie);
			return (value != null) ? unescape(value[1]) : null; 
		}
		
		function setCookie(name, value) {
			  var today = new Date(); 
			  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
			  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
		 }
		
		function clearCookie(){
			deleteCookie("select_class");
			deleteCookie("select_subject");
			alert("Cookies Deleted");
		}
		
		function deleteCookie(name)
		{
			var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
			document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
		}		
	</script>
		
</body>
</html>